package hos.thread.hander

import hos.thread.RunnableThis
import hos.thread.ThreadOption

/**
 * <p>Title: MH </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2023/8/4 20:28
 * @version : 1.0
 */

object MH {

    @JvmStatic
    fun getHandler(): MainHandler {
        return MainHandler.handler
    }

    @JvmStatic
    @JvmOverloads
    inline fun run(
        handle: ThreadOption.UI = ThreadOption.UI.ON,
        time: Long = 300,
        crossinline block: MainHandler.() -> Unit,
    ): MainHandler {
        return MainHandler.run(handle, time, block)
    }

    /**
     * Returns true if the current thread is the main thread, false otherwise.
     *
     * @return true if we are on the main thread, false otherwise.
     */
    @JvmStatic
    fun isMainThread(): Boolean {
        return MainHandler.isMainThread()
    }

    @JvmStatic
    fun clear() {
        MainHandler.handler.clearCallback()
    }

    /**
     * 延迟在主线程执行
     *
     * @param runnable    运行
     * @param delayMillis 延迟时间
     */
    @JvmStatic
    @JvmOverloads
    fun postDelayed(delayMillis: Long = 300, runnable: RunnableThis): Boolean {
        return MainHandler.handler.postDelayed(runnable, delayMillis)
    }

    /**
     * 指定时间执行
     *
     * @param runnable     主线程
     * @param uptimeMillis 设定时间
     */
    @JvmStatic
    fun postAtTime(uptimeMillis: Long, runnable: RunnableThis): Boolean {
        return MainHandler.handler.postAtTime(runnable, uptimeMillis)
    }

    /**
     * 切换到主线程
     *
     * @param runnable 主线程
     */
    @JvmStatic
    fun postToMain(runnable: RunnableThis) {
        MainHandler.handler.postToMain(runnable)
    }

    /**
     * 在主线程上运行
     *
     * @param runnable 主线程
     */
    @JvmStatic
    fun postOnMain(runnable: RunnableThis) {
        MainHandler.handler.postOnMain(runnable)
    }


    @JvmStatic
    fun remove(runnable: Runnable) {
        MainHandler.handler.removeCallbacks(runnable)
    }


}